package com.jz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jz.mapper.auto.TTaskMapper;
import com.jz.module.dto.TTaskDto;
import com.jz.module.dto.base.PageInfoDto;
import com.jz.module.entity.TTask;
import com.jz.module.entity.TTaskExample;
import com.jz.service.TaskService;
import com.jz.util.ResultUtil;
import com.jz.util.SysCode;
import com.jz.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    @SuppressWarnings("all")
    private TTaskMapper tTaskMapper;

    @Override
    public ResultUtil page(TTaskDto dto) {
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        TTaskExample example = new TTaskExample();
        example.setOrderByClause("update_time desc,create_time desc");
        List<TTask> tTasks = tTaskMapper.selectByExample(example);
        PageInfo<TTask> info = new PageInfo<>(tTasks);
        PageInfoDto<TTaskDto> result = new PageInfoDto<TTaskDto>();
        BeanUtils.copyProperties(info, result);
        return ResultUtil.success(result);
    }

    @Override
    public ResultUtil query(TTaskDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        TTask tTask = tTaskMapper.selectByPrimaryKey(dto.getId());
        if (tTask == null) {
            return ResultUtil.error(SysCode.NO_DATA);
        }
        TTaskDto result = new TTaskDto();
        BeanUtils.copyProperties(tTask, result);
        return ResultUtil.success(result);
    }

    @Override
    public ResultUtil save(TTaskDto dto) {
        if (dto == null || StringUtils.isAnyBlank(
                dto.getTitle()
        )) {
            return ResultUtil.paramError();
        }
        if (StringUtils.isBlank(dto.getId())) {
            // insert
            TTask insert = new TTask();
            BeanUtils.copyProperties(dto, insert);

            insert.setId(UUIDUtil.getUUID());
            insert.setCreateTime(new Date());
            insert.setUpdateTime(new Date());
            int res = tTaskMapper.insert(insert);
            if (res < 1) {
                return ResultUtil.error(SysCode.INSERT_ERROR);
            }
            return ResultUtil.success();
        } else {
            TTask update = new TTask();
            BeanUtils.copyProperties(dto, update);
            update.setUpdateTime(new Date());
            int res = tTaskMapper.updateByPrimaryKeySelective(update);
            if (res < 1) {
                return ResultUtil.error(SysCode.UPDATE_ERROR);
            }
        }
        return ResultUtil.success();
    }

    @Override
    public ResultUtil delete(TTaskDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        int i = tTaskMapper.deleteByPrimaryKey(dto.getId());
        if (i > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error(SysCode.DELETE_ERROR);
    }
}
